package com.ruoyi.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.page.PageUtils;
import com.ruoyi.common.utils.page.Query;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.domain.vo.QueryVo;
import com.ruoyi.web.domain.vo.UcFeedbackVo;
import com.ruoyi.web.domain.wx.AppQueryVo;
import com.ruoyi.web.mapper.*;
import com.ruoyi.web.service.IUcFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class UcFeedbackServiceImpl extends ServiceImpl<UcFeedbackMapper, UcFeedback> implements IUcFeedbackService {


    @Override
    public PageUtils listByPage(QueryVo queryVo) {
        QueryWrapper<UcFeedback> qw =new QueryWrapper<>();
        if(!StringUtils.isEmpty(queryVo.getQKey())){
            qw.and((wrapper)->{
                wrapper.or().like("nick_name",queryVo.getQKey());
            });
        }
        if(!StringUtils.isEmpty(queryVo.getQTimeStartList())){
            qw.le("create_time",queryVo.getQTimeStartList().get(1)+" 23:59:59");
            qw.ge("create_time",queryVo.getQTimeStartList().get(0)+" 00:00:00");
        }

        IPage<UcFeedback> page = this.page(new Query<UcFeedback>().getPage(BeanUtil.beanToMap(queryVo)), qw);
        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void insert(UcFeedback ucFeedback) {
//        Date date = new Date();
        SysUser user = SecurityUtils.getLoginUser().getUser();

        ucFeedback.setDelStatus(1);
        ucFeedback.setUserId(user.getUserId());
        ucFeedback.setUserName(user.getUserName());
        ucFeedback.setNickName(user.getNickName());
        ucFeedback.setPhonenumber(user.getPhonenumber());

        baseMapper.insert(ucFeedback);
    }

    @Override
    public int delete(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Override
    public void reply(UcFeedbackVo ucFeedback) {
        UcFeedback oldUcFeedback = baseMapper.selectById(ucFeedback.getId());
        oldUcFeedback.setNotes(ucFeedback.getReply());
        baseMapper.updateById(oldUcFeedback);
    }

    @Override
    public PageUtils appListByPage(AppQueryVo appQueryVo) {
        QueryWrapper<UcFeedback> qw =new QueryWrapper<>();
        if(appQueryVo.getQUserId()!=null){
            qw.eq("user_id",appQueryVo.getQUserId());
        }
        if(appQueryVo.getQYear()!=null){
            qw.eq("year",appQueryVo.getQYear());
        }
        if(appQueryVo.getQQuarter()!=null){
            qw.eq("quarter",appQueryVo.getQQuarter());
        }
        if(appQueryVo.getQTimeStart()!=null){
            qw.ge("time_start",appQueryVo.getQTimeStart() + " 00:00:00");
        }
        if(appQueryVo.getQTimeEnd()!=null) {
            qw.le("time_start", appQueryVo.getQTimeEnd() + " 23:59:59");
        }

        if("descending".equals(appQueryVo.getIsAsc())){
            qw.orderBy(true,false,appQueryVo.getOrderByColumn()).orderBy(true,false,"id");
        }else if("ascending".equals(appQueryVo.getIsAsc())){
            qw.orderBy(true,true,appQueryVo.getOrderByColumn()).orderBy(true,false,"id");
        }else{
            qw.orderBy(true,false,"id");
        }

        IPage<UcFeedback> page = this.page(new Query<UcFeedback>().getPage(BeanUtil.beanToMap(appQueryVo)), qw);
        return new PageUtils(page);
    }

}
